package edu.snake.implement.model.character.movement;

import edu.snake.core.model.arena.Arena;
import edu.snake.core.model.arena.Position;
import edu.snake.core.model.character.Character;
import edu.snake.core.service.LiveOrDieService;
import edu.snake.implement.model.arena.Direction;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static edu.snake.implement.model.arena.PositionFactory.getNewPosition;


public class RandomMoveSimple extends AbstractMoveSimple {

    public RandomMoveSimple(Character character, Arena arena, LiveOrDieService liveOrDieService) {
        super(character, arena, liveOrDieService);
    }

    protected Direction calculateNewDirection(List<Direction> positionList) {
        switch (positionList.size()) {
            case 0:
                return null;
            case 1:
                return positionList.get(0);
            default:
                int random = new Random().nextInt(positionList.size());
                return positionList.get(random);
        }
    }

    protected List<Direction> lookingArena(Arena arena, Character character) {
        Position currentPosition = arena.get(character);
        if (currentPosition == null) {
            throw new RuntimeException("Character not found in Arena" + character.toString());
        }

        Position lookingPosition = getNewPosition(currentPosition, Direction.LEFT);
        List<Direction> result = new ArrayList<Direction>();
        if (!arena.checkArenaBorder(lookingPosition) && arena.get(lookingPosition) == null) {
            result.add(Direction.LEFT);
        }
        lookingPosition = getNewPosition(currentPosition, Direction.UP);
        if (!arena.checkArenaBorder(lookingPosition) && arena.get(lookingPosition) == null) {
            result.add(Direction.UP);
        }
        lookingPosition = getNewPosition(currentPosition, Direction.RIGHT);
        if (!arena.checkArenaBorder(lookingPosition) && arena.get(lookingPosition) == null) {
            result.add(Direction.RIGHT);
        }
        lookingPosition = getNewPosition(currentPosition, Direction.DOWN);
        if (!arena.checkArenaBorder(lookingPosition) && arena.get(lookingPosition) == null) {
            result.add(Direction.DOWN);
        }
        return result;
    }
}
